package xML;

import noyau.Question;
import noyau.Test;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: matthieujimenez
 * Date: 03/04/12
 * Time: 18:32
 * To change this template use File | Settings | File Templates.
 */
public class XMLCreation {

        private static Element racine;
        private static org.jdom.Document document;
        private int nbquestion;
    private Element test;



    // on hérite de toute les données du serveur
        private ArrayList<Test> testchar;
        public XMLCreation() {
            super();
            racine = new Element("Racine");
            document = new Document(racine);

            //this.nbquestion=nbquestion;

        }
        public void ajouterTest (String nomtest) throws IOException {
            Element test=new Element("Test");
            racine.addContent(test);
            Element nomduTest=new Element("NomDuTest");
            test.addContent(nomduTest);
            nomduTest.setText(nomtest);
            Element question=new Element("Question");
            test.addContent(question);
            enregistre();

        }
        public void sauvegarder(Question ques,String nomtest) throws IOException {
                List listTest=racine.getChildren("Test");
            String nomObtenu;
            Element test;
            for (Object aListTest : listTest) {

                Element courant = (Element) aListTest;
                Element nom= ((Element) aListTest).getChild("NomDuTest");
                if (nom.getText().equals(nomtest)){
                      test=courant;
                    Element questi =test.getChild("Question") ;

                    Element question=new Element("question");
                    questi.addContent(question);


                    Element intitule=new Element("Intitule");
                  question.addContent(intitule);
                    intitule.setText(""+ques.getIntitule());

                    Element reponse =new Element("Reponse") ;
                  question.addContent(reponse);

                    for (int i=0; i<4;i++){
                        Element repon=new Element("reponse");
                        reponse.addContent(repon);
                        repon.setText(""+ques.getReponse()[i]);

                    }

                    Element bonnerep=new Element("BonRep");
                    question.addContent(bonnerep);
                    bonnerep.setText(""+ques.getBonneReponse());


                    enregistre();

                }
            }







        }

       public boolean charger() throws Exception {
                testchar=new ArrayList<Test>();
                String tab[]=new String[4];
            try {
                lireFichier();

                List listTest=racine.getChildren("Test");


                for (Object aListTs:listTest){

                    Element courtest=(Element) aListTs;


                    Test testc=new Test(courtest.getChild("NomDuTest").getText());

                     Element question=courtest.getChild("Question");


               // mesTrajets = new ArrayList<Trajet>();
               // mesVehicules = new Array                 Element question=courtest.getChilList<Vehicule>();




                List listquestion = question.getChildren("question");


                for (Object aListquestion : listquestion) {

                    Element courant = (Element) aListquestion;


                    String inti = courant.getChildText("Intitule");


                    Element rep = courant.getChild("Reponse");
                    List listreponse = rep.getChildren("reponse");
                    Iterator i = listreponse.iterator();
                    for (int k = 0; k < 4; k++) {
                        Element cour = (Element) i.next();

                        tab[k] = cour.getText();

                    }
                    int bonner = Integer.parseInt(courant
                            .getChildText("BonRep"));

                    Question ques = new Question(inti, tab[0], tab[1], tab[2], tab[3], bonner);
                    testc.ajoutQuestion(ques);



                }
                testchar.add(testc);



                }
                return true;
            } catch (Exception e) {

                System.out.println("Problème de lecture. ");
                return false;
            }

        }


        private void enregistre() throws IOException {

            XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());

            sortie.output(document, new FileOutputStream("dataV3.xml"));
        }

        private void lireFichier() throws Exception {
            SAXBuilder sxb = new SAXBuilder();
            try {
                document = sxb.build(new File("dataV3.xml"));
            } catch (JDOMException e) {
                e.printStackTrace();  //To changere body of catch statement use File | Settings | File Templates.
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            racine = document.getRootElement();
        }

    public ArrayList<Test> getTestchar() {
        return testchar;
    }



}
